package org.springblade.modules.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.app.dto.QpcDto;
import org.springblade.modules.app.entity.Xfc;
import org.springblade.modules.app.service.IXfcService;
import org.springblade.modules.system.entity.Region;
import org.springblade.modules.system.service.IRegionService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@RestController
@AllArgsConstructor
@RequestMapping("blade-app/xfc")
public class XfcController extends BladeController {

    private final IXfcService xfcService;


    private final IRegionService regionService;

    /**
     * 详情
     */
    @GetMapping("/detail")
    public R<Xfc> detail(Xfc xfc) {
        if (StringUtils.isEmpty(xfc.getId())) {
            return R.fail("ID Must not be null");
        }
        Xfc detail = xfcService.getOne(Condition.getQueryWrapper(xfc));
        return R.data(detail);
    }

    /**
     * 分页 秀风采 谁来
     */
    @GetMapping("/list")
    public List<Xfc> list(Xfc xfc) {
        LambdaQueryWrapper<Xfc> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(xfc.getProvcode())) {
            queryWrapper.and(wrapper -> wrapper.eq(Xfc::getProvcode, xfc.getProvcode()));
        }
        if (!StringUtils.isEmpty(xfc.getCitycode())) {
            queryWrapper.and(wrapper -> wrapper.eq(Xfc::getCitycode, xfc.getCitycode()));
        }
        if (!StringUtils.isEmpty(xfc.getAreacode())) {
            queryWrapper.and(wrapper -> wrapper.eq(Xfc::getAreacode, xfc.getAreacode()));
        }
        queryWrapper.orderByDesc(Xfc::getCtime);
        List<Xfc> list = xfcService.list(queryWrapper);
        return list;
    }

    /**
     * 自定义分页 秀风采 谁来
     */
    @GetMapping("/page")
    public R<IPage<Xfc>> page(Page page, Xfc xfc) {
        LambdaQueryWrapper<Xfc> xfcqueryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(xfc.getName())) {
            xfcqueryWrapper.and(wrapper -> wrapper.like(Xfc::getName, xfc.getName()));
        }
        xfcqueryWrapper.orderByDesc(Xfc::getCtime);
        IPage<Xfc> pages = xfcService.page(page, xfcqueryWrapper);
        return R.data(pages);
    }


    @PostMapping("/save")
    public R save(@RequestBody Xfc xfc) {


        Region regionP = regionService.getById(xfc.getProvcode());
        xfc.setProvname(regionP.getName());

        if (!StringUtils.isEmpty(xfc.getCitycode())) {
            Region regionC = regionService.getById(xfc.getCitycode());
            xfc.setCityname(regionC.getName());
        }

        if (!StringUtils.isEmpty(xfc.getAreacode())) {
            Region regionA = regionService.getById(xfc.getAreacode());
            xfc.setAreaname(regionA.getName());
        }


        return R.status(xfcService.save(xfc));
    }


    /**
     * 修改 秀风采 谁来
     */
    @PostMapping("/update")
    public R update(@RequestBody Xfc xfc) {

        Region regionP = regionService.getById(xfc.getProvcode());
        xfc.setProvname(regionP.getName());

        if (!StringUtils.isEmpty(xfc.getCitycode())) {
            Region regionC = regionService.getById(xfc.getCitycode());
            xfc.setCityname(regionC.getName());
        }

        if (!StringUtils.isEmpty(xfc.getAreacode())) {
            Region regionA = regionService.getById(xfc.getAreacode());
            xfc.setAreaname(regionA.getName());
        }

        return R.status(xfcService.updateById(xfc));
    }

    /**
     * 新增或修改 秀风采 谁来
     */
    @PostMapping("/submit")
    public R submit(@RequestBody Xfc xfc) {

        Region regionP = regionService.getById(xfc.getProvcode());
        xfc.setProvname(regionP.getName());

        if (!StringUtils.isEmpty(xfc.getCitycode())) {
            Region regionC = regionService.getById(xfc.getCitycode());
            xfc.setCityname(regionC.getName());
        }

        if (!StringUtils.isEmpty(xfc.getAreacode())) {
            Region regionA = regionService.getById(xfc.getAreacode());
            xfc.setAreaname(regionA.getName());
        }

        return R.status(xfcService.saveOrUpdate(xfc));
    }


    /**
     * 删除 秀风采 谁来
     */
    @PostMapping("/remove")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        if (StringUtils.isEmpty(ids)) {
            return R.fail("ID Must not be null");
        }
        return R.status(xfcService.removeByIds(Func.toLongList(ids)));
    }


    @GetMapping("/getQpcByAreaCode")
    public List<QpcDto> getQpcByAreaCode(Region region) {
        List<QpcDto> result = new ArrayList<>();
        List<Xfc> cityList = xfcService.list(new LambdaQueryWrapper<Xfc>().eq(Xfc::getAreacode, region.getCode()));
        for (Xfc entity : cityList) {
            QpcDto single = new QpcDto();
            single.setCode(entity.getId() + "");
            single.setName(entity.getName());
            result.add(single);
        }
        return result;
    }

}
